﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Linq;
using intelliPaper.Model;
using intelliPaper.DB;

namespace intelliSys.Management.Automation.Test
{
    /// <summary>
    /// Interaction logic for GenericList.xaml
    /// </summary>
    public partial class GenericList : Window
    {
        object dialogueResult;
        public SelectionMode ListSelectionMode
        {
            get;
            set;
        }
        public object DialogueResult
        {
            get
            {
                return dialogueResult;
            }
        }
        public GenericList()
        {
            this.InitializeComponent();
            this.Loaded += new RoutedEventHandler(GenericList_Loaded);
            this.MouseLeftButtonDown += new MouseButtonEventHandler(GenericList_MouseLeftButtonDown);
            ListSelectionMode = SelectionMode.Single;
            list.SelectionMode = SelectionMode.Single;
            // Insert code required on object creation below this point.
        }

        void GenericList_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        void GenericList_Loaded(object sender, RoutedEventArgs e)
        {
            list.SelectionMode = ListSelectionMode;
        }

        private void Comfirm_Click(object sender, RoutedEventArgs e)
        {
            if (list.SelectionMode == SelectionMode.Single)
            {
                if (list.SelectedItem == null)
                {
                    Message.Show(this, "空选择", MessageType.Comfirm, "未选取任何项目", "选择一项以继续");
                    return;
                }
                dialogueResult = list.SelectedItem;
                this.Close();
            }
            if (list.SelectionMode == SelectionMode.Extended)
            {
                if (list.SelectedItems == null)
                {
                    Message.Show(this, "空选择", MessageType.Comfirm, "未选取任何项目", "选择一项或多项以继续");
                    return;
                }
                dialogueResult = list.SelectedItems;
                this.Close();
            }
        }

        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
        IEnumerable<intelliPaper.Model.ISearchable> currentSource;
        private void LoadQuestions()
        {
            list.ItemsSource = currentSource = (IEnumerable<intelliPaper.Model.ISearchable>)DataRepository.Questions;
        }
        private void LoadPapers()
        {
            list.ItemsSource = currentSource = (IEnumerable<intelliPaper.Model.ISearchable>)DataRepository.Papers;
        }
        private void LoadKnowledgePoints()
        {
            list.ItemsSource = currentSource = (IEnumerable<intelliPaper.Model.ISearchable>)DataRepository.KnowledgePoints;
        }
        private void commandBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (commandBox.SelectedIndex == 0)
            {
                LoadQuestions();
            }
            if (commandBox.SelectedIndex == 2)
            {
                LoadKnowledgePoints();
            }
            if (commandBox.SelectedIndex == 1)
            {
                LoadPapers();
            }
        }

        private void TextBox_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (SearchBox.FontStyle == FontStyles.Italic)
            {
                SearchBox.Text = "";
                SearchBox.FontStyle = FontStyles.Normal;
            }
        }

        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (currentSource == null)
            {
                return;
            }
            if (SearchBox.Text.Length == 0)
            {
                list.ItemsSource = currentSource;
            }
            else
            {
                list.ItemsSource = currentSource.Where<intelliPaper.Model.ISearchable>(o =>
                    {
                        return o.SearchTag.Contains(SearchBox.Text);
                    });
            }
        }

        private void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            object o = (sender as CheckBox).DataContext;
            list.SelectedItem = o;
            if ((sender as CheckBox).IsChecked.Value)
            {
                if (o.GetType() == typeof(KnowledgePoint))
                {
                    Dao.DeleteKnowledgePoint(o as KnowledgePoint);
                }
                if (o.GetType() == typeof(Question))
                {
                    Dao.DeleteQuestion(o as Question);
                }
                if (o.GetType() == typeof(Paper))
                {
                    Dao.DeletePaper(o as Paper);
                }
            }
            else
            {
                if (o.GetType() == typeof(KnowledgePoint))
                {
                    Dao.RecoverKnowledgePoint(o as KnowledgePoint);
                }
                if (o.GetType() == typeof(Question))
                {
                    Dao.RecoverQuestion(o as Question);
                }
                if (o.GetType() == typeof(Paper))
                {
                    Dao.RecoverPaper(o as Paper);
                }
            }
        }
    }
}